<?php
if( is_admin() ) {

	/* Start of: WordPress Administration */

	/* WordPress Administration menu */
	function wpsc_st_add_modules_admin_pages( $page_hooks, $base_page ) {

		$page_hooks[] = add_submenu_page( $base_page, __( 'Store Toolkit', 'wpsc_st' ), __( 'Store Toolkit', 'wpsc_st' ), 7, 'wpsc_st', 'wpsc_st_html_page' );
		return $page_hooks;

	}
	add_filter( 'wpsc_additional_pages', 'wpsc_st_add_modules_admin_pages', 10, 2 );

	function wpsc_st_aioseop_init_meta_box() {

		$pagename = 'wpsc-product';
		add_meta_box( 'wpsc_st_aioseop_meta_box', __( 'All in One SEO Pack', 'wpsc_st' ), 'wpsc_st_aioseop_meta_box', $pagename, 'normal', 'default' );

	}
	if( function_exists( 'aioseop_get_version' ) ) {
		add_action( 'admin_menu', 'wpsc_st_aioseop_init_meta_box' );
		add_filter( 'wpsc_products_page_forms', 'wpsc_st_aioseop_add_to_product_form' );
	}

	function wpsc_st_aioseop_add_to_product_form( $order ) {

		if( array_search( 'wpsc_st_aioseop_meta_box', (array)$order['side'] ) === false )
			$order['side'][] = 'wpsc_st_aioseop_meta_box';

		return $order;

	}

	function wpsc_st_aioseop_meta_box() {

		global $post, $wpdb, $closed_postboxes, $wpsc_st;

		$title = get_post_meta( $post->ID, '_aioseop_title', true );
		$description = get_post_meta( $post->ID, '_aioseop_description', true );
		$keywords = get_post_meta( $post->ID, '_aioseop_keywords', true );
		$title_atr = get_post_meta( $post->ID, '_aioseop_titleatr', true );
		$menu_label = get_post_meta( $post->ID, '_aioseop_menulabel', true );

		include_once( $wpsc_st['abspath'] . '/templates/admin/wpsc-admin_st_aioseop_38.php' );

	}

	/* WordPress Administration menu */
	function wpsc_st_wpsc_admin_html_page() {

		global $wpsc_st;

		include_once( $wpsc_st['abspath'] . '/templates/admin/wpsc-admin_store_st-permalinks.php' );

	}
	add_action( 'wpsc_admin_settings_page', 'wpsc_st_wpsc_admin_html_page' );

	function wpsc_st_return_count( $dataset ) {

		global $wpdb;

		$count_sql = null;
		switch( $dataset ) {

			/* WP e-Commerce */

			case 'products':
				$post_type = 'wpsc-product';
				$count = wp_count_posts( $post_type );
				break;

			case 'variations':
				$post_type = 'wpsc-variation';
				$count = wp_count_posts( $post_type );
				break;

			case 'images':
				$post_type = 'wpsc-product';
				$products = (array)get_posts( array(
					'post_type' => $post_type,
					'post_status' => wpsc_st_post_statuses(),
					'numberposts' => -1
				) );
				$count = 0;
				if( $products ) {
					foreach( $products as $product ) {
						$args = array(
							'post_type' => 'attachment',
							'post_parent' => $product->ID,
							'post_status' => 'inherit',
							'post_mime_type' => 'image',
							'numberposts' => -1
						);
						$images = get_children( $args );
						if( $images )
							$count = $count + count( $images );
					}
				}
				break;

			case 'files':
				$post_type = 'wpsc-product-file';
				$count = wp_count_posts( $post_type );
				break;

			case 'categories':
				$term_taxonomy = 'wpsc_product_category';
				$count = wp_count_terms( $term_taxonomy );
				break;

			case 'tags':
				$term_taxonomy = 'product_tag';
				$count = wp_count_terms( $term_taxonomy );
				break;

			case 'orders':
				$count_sql = "SELECT COUNT(`id`) FROM `" . $wpdb->prefix . "wpsc_purchase_logs`";
				break;

			case 'coupons':
				$count_sql = "SELECT COUNT(`id`) FROM `" . $wpdb->prefix . "wpsc_coupon_codes`";
				break;

			/* 3rd Party */

			case 'wishlist':
				$post_type = 'wpsc-wishlist';
				$count = wp_count_posts( $post_type );
				break;

			case 'enquiries':
				$post_type = 'wpsc-enquiry';
				$count = wp_count_posts( $post_type );
				break;

			case 'credit-cards':
				$count_sql = "SELECT COUNT(`id`) FROM `" . $wpdb->prefix . "wpsc_creditcard`";
				break;

			case 'custom-fields':
				$custom_fields = get_option( 'wpsc_cf_data' );
				if( $custom_fields )
					$count = count( maybe_unserialize( $custom_fields ) );
				else
					$count = 0;
				break;

			/* WordPress */

			case 'posts':
				$post_type = 'post';
				$count = wp_count_posts( $post_type );
				break;

			case 'post_categories':
				$term_taxonomy = 'category';
				$count = wp_count_terms( $term_taxonomy );
				break;

			case 'post_tags':
				$term_taxonomy = 'post_tag';
				$count = wp_count_terms( $term_taxonomy );
				break;

			case 'links':
				$count_sql = "SELECT COUNT(`link_id`) FROM `" . $wpdb->prefix . "links`";
				break;

			case 'comments':
				$count = wp_count_comments();
				break;

		}
		if( isset( $count ) || $count_sql ) {
			if( isset( $count ) ) {
				if( is_object( $count ) ) {
					$count_object = $count;
					$count = 0;
					foreach( $count_object as $key => $item )
						$count = $item + $count;
				}
				return $count;
			} else {
				$count = $wpdb->get_var( $count_sql );
			}
			return $count;
		} else {
			return 0;
		}

	}

	function wpsc_st_clear_dataset( $dataset ) {

		global $wpdb;

		switch( $dataset ) {

			/* WP e-Commerce */

			case 'products':
				$post_type = 'wpsc-product';
				$products = (array)get_posts( array(
					'post_type' => $post_type,
					'post_status' => wpsc_st_post_statuses(),
					'numberposts' => -1
				) );
				if( $products ) {
					foreach( $products as $product )
						wp_delete_post( $product->ID, true );
				}
				break;

			case 'variations':
				$term_taxonomy = 'wpsc-variation';
				$variations = get_terms( $term_taxonomy, array( 'hide_empty' => false ) );
				if( $variations ) {
					foreach( $variations as $variation )
						wp_delete_term( $variation->term_id, $term_taxonomy );
				}
				delete_option( 'wpsc-variation_children' );
				break;

			case 'categories':
				$term_taxonomy = 'wpsc_product_category';
				$categories = get_terms( $term_taxonomy, array( 'hide_empty' => false ) );
				if( $categories ) {
					foreach( $categories as $category ) {
						wp_delete_term( $category->term_id, $term_taxonomy );
						$wpdb->query( "DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = " . $category->term_id );
						$wpdb->query( "DELETE FROM `" . $wpdb->term_relationships . "` WHERE `term_taxonomy_id` = " . $category->term_taxonomy_id );
					}
				}
				$wpdb->query( "DELETE FROM `" . $wpdb->prefix . "wpsc_meta` WHERE `object_type` = 'wpsc_category'" );
				$wpdb->query( "DELETE FROM `" . $wpdb->term_taxonomy . "` WHERE `taxonomy` = '" . $term_taxonomy . "'" );
				break;

			case 'tags':
				$term_taxonomy = 'product_tag';
				$tags = get_terms( $term_taxonomy, array( 'hide_empty' => false ) );
				if( $tags ) {
					foreach( $tags as $tag ) {
						wp_delete_term( $tag->term_id, $term_taxonomy );
						$wpdb->query( "DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = " . $tag->term_id );
					}
				}
				break;

			case 'images':
				$post_type = 'wpsc-product';
				$products = (array)get_posts( array(
					'post_type' => $post_type,
					'post_status' => wpsc_st_post_statuses(),
					'numberposts' => -1
				) );
				if( $products ) {
					$upload_dir = wp_upload_dir();
					foreach( $products as $product ) {
						$args = array(
							'post_type' => 'attachment',
							'post_parent' => $product->ID,
							'post_status' => 'inherit',
							'post_mime_type' => 'image',
							'numberposts' => -1
						);
						$images = get_children( $args, ARRAY_A );
						if( $images ) {
							// $intermediate_sizes = wpsc_intermediate_image_sizes_advanced( $intermediate_sizes );
							foreach( $images as $image ) {
								wp_delete_attachment( $image['ID'], true );
/*
								$image->filepath = dirname( $upload_dir['basedir'] . '/' . get_post_meta( $image->ID, '_wp_attached_file', true ) );
								chdir( $image->filepath );
								$image->filename = basename( get_post_meta( $image->ID, '_wp_attached_file', true ) );
								$image->extension = strrchr( $image->filename, '.' );
								$image->filebase = wpsc_st_remove_filename_extension( $image->filename );
								foreach( $intermediate_sizes as $intermediate_size ) {
									if( file_exists( $image->filebase . '-' . $intermediate_size['width'] . 'x' . $intermediate_size['height'] . $image->extension ) )
										@unlink( $image->filebase . '-' . $intermediate_size['width'] . 'x' . $intermediate_size['height'] . $image->extension );
								}
								if( file_exists( $image->filename ) )
									@unlink( basename( $image->filename ) );
								wp_delete_post( $image->ID );
*/
							}
							unset( $images, $image );
						}
					}
				}
				break;

			case 'files':
				$post_type = 'wpsc-product-file';
				$files = (array)get_posts( array(
					'post_type' => $post_type,
					'post_status' => wpsc_st_post_statuses(),
					'numberposts' => -1
				) );
				if( $files ) {
					foreach( $files as $file ) {
						if( isset( $file->ID ) )
							wp_delete_post( $file->ID, true );
					}
				}
				break;

			case 'orders':
				$wpdb->query( "TRUNCATE TABLE `" . $wpdb->prefix . "wpsc_purchase_logs`" );
				$wpdb->query( "TRUNCATE TABLE `" . $wpdb->prefix . "wpsc_cart_contents`" );
				$wpdb->query( "TRUNCATE TABLE `" . $wpdb->prefix . "wpsc_submited_form_data`" );
				$wpdb->query( "TRUNCATE TABLE `" . $wpdb->prefix . "wpsc_download_status`" );
				$wpdb->query( "DELETE FROM `" . $wpdb->prefix . "wpsc_meta` WHERE `object_type` = 'wpsc_cart_item'" );
				break;

			case 'coupons':
				$wpdb->query( "TRUNCATE TABLE `" . $wpdb->prefix . "wpsc_coupon_codes`" );
				break;

			/* 3rd Party */

			case 'wishlist':
				$post_type = 'wpsc-wishlist';
				$wishlists = (array)get_posts( array( 
					'post_type' => $post_type,
					'post_status' => wpsc_st_post_statuses(),
					'numberposts' => -1
				) );
				if( $wishlists ) {
					foreach( $wishlists as $wishlist ) {
						if( isset( $wishlist->ID ) )
							wp_delete_post( $wishlist->ID, true );
					}
				}
				break;

			case 'enquiries':
				$post_type = 'wpsc-enquiry';
				$enquiries = (array)get_posts( array( 
					'post_type' => $post_type,
					'post_status' => wpsc_st_post_statuses(),
					'numberposts' => -1
				) );
				if( $enquiries ) {
					foreach( $enquiries as $enquiry ) {
						if( isset( $enquiry->ID ) )
							wp_delete_post( $enquiry->ID, true );
					}
				}
				break;

			case 'credit-cards':
				$wpdb->query( "TRUNCATE TABLE `" . $wpdb->prefix . "wpsc_creditcard`" );
				break;

			case 'custom-fields':
				delete_option( 'wpsc_cf_data' );
				break;

			/* WordPress */

			case 'posts':
				$post_type = 'post';
				$posts = (array)get_posts( array( 
					'post_type' => $post_type,
					'post_status' => wpsc_st_post_statuses(),
					'numberposts' => -1
				) );
				if( $posts ) {
					foreach( $posts as $post ) {
						if( isset( $post->ID ) )
							wp_delete_post( $post->ID, true );
					}
				}
				break;

			case 'post_categories':
				$term_taxonomy = 'category';
				$post_categories = get_terms( $term_taxonomy, array( 'hide_empty' => false ) );
				if( $post_categories ) {
					foreach( $post_categories as $post_category ) {
						wp_delete_term( $post_category->term_id, $term_taxonomy );
						$wpdb->query( "DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = " . $post_category->term_id );
						$wpdb->query( "DELETE FROM `" . $wpdb->term_relationships . "` WHERE `term_taxonomy_id` = " . $post_category->term_taxonomy_id );
					}
				}
				$wpdb->query( "DELETE FROM `" . $wpdb->term_taxonomy . "` WHERE `taxonomy` = '" . $term_taxonomy . "'" );
				break;

			case 'post_tags':
				$term_taxonomy = 'post_tag';
				$post_tags = get_terms( $term_taxonomy, array( 'hide_empty' => false ) );
				if( $post_tags ) {
					foreach( $post_tags as $post_tag ) {
						wp_delete_term( $post_tag->term_id, $term_taxonomy );
						$wpdb->query( "DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = " . $post_tag->term_id );
						$wpdb->query( "DELETE FROM `" . $wpdb->term_relationships . "` WHERE `term_taxonomy_id` = " . $post_tag->term_taxonomy_id );
					}
				}
				$wpdb->query( "DELETE FROM `" . $wpdb->term_taxonomy . "` WHERE `taxonomy` = '" . $term_taxonomy . "'" );
				break;

			case 'links':
				$wpdb->query( "TRUNCATE TABLE `" . $wpdb->prefix . "links`" );
				break;

			case 'comments':
				$comments = get_comments();
				if( $comments ) {
					foreach( $comments as $comment ) {
						if( $comment->comment_ID )
							wp_delete_comment( $comment->comment_ID, true );
					}
				}
				break;

		}

	}

	function wpsc_st_pd_create_product_addons( $product, $import ) {

		if( isset( $product->aioseop_keywords ) || isset( $product->aioseop_description ) || isset( $product->aioseop_title ) || isset( $product->aioseop_titleatr ) || isset( $product->aioseop_menulabel ) ) {
			if( !isset( $product->aioseop_keywords ) )
				$product->aioseop_keywords = '';
			if( !isset( $product->aioseop_description ) )
				$product->aioseop_description = '';
			if( !isset( $product->aioseop_title ) )
				$product->aioseop_title = '';
			if( !isset( $product->aioseop_titleatr ) )
				$product->aioseop_titleatr = '';
			if( !isset( $product->aioseop_menulabel ) )
				$product->aioseop_menulabel = '';
			update_post_meta( $product->ID, '_aioseop_keywords', $product->aioseop_keywords );
			update_post_meta( $product->ID, '_aioseop_description', $product->aioseop_description );
			update_post_meta( $product->ID, '_aioseop_title', $product->aioseop_title );
			update_post_meta( $product->ID, '_aioseop_titleatr', $product->aioseop_titleatr );
			update_post_meta( $product->ID, '_aioseop_menulabel', $product->aioseop_menulabel );
		}
		return $product;

	}
	add_filter( 'wpsc_pd_create_product_addons', 'wpsc_st_pd_create_product_addons', null, 2 );

	function wpsc_st_pd_merge_product_addons( $product, $import, $product_data ) {

		if( isset( $product->aioseop_keywords ) && $product->aioseop_keywords || isset( $product->aioseop_description ) && $product->aioseop_description || isset( $product->aioseop_title ) && $product->aioseop_title || isset( $product->aioseop_titleatr ) && $product->aioseop_titleatr || isset( $product->aioseop_menulabel ) && $product->aioseop_menulabel ) {
			if( isset( $product->aioseop_keywords ) && $product->aioseop_keywords ) {
				if( $product_data->aioseop_keywords <> $product->aioseop_keywords ) {
					update_post_meta( $product->ID, '_aioseop_keywords', $product->aioseop_keywords );
					$product->updated = true;
				}
			}
			if( isset( $product->aioseop_description ) && $product->aioseop_description ) {
				if( $product_data->aioseop_description <> $product->aioseop_description ) {
					update_post_meta( $product->ID, '_aioseop_description', $product->aioseop_description );
					$product->updated = true;
				}
			}
			if( isset( $product->aioseop_title ) && $product->aioseop_title ) {
				if( $product_data->aioseop_title <> $product->aioseop_title ) {
					update_post_meta( $product->ID, '_aioseop_title', $product->aioseop_title );
					$product->updated = true;
				}
			}
			if( isset( $product->aioseop_titleatr ) && $product->aioseop_titleatr ) {
				if( $product_data->aioseop_titleatr <> $product->aioseop_titleatr ) {
					update_post_meta( $product->ID, '_aioseop_titleatr', $product->aioseop_titleatr );
					$product->updated = true;
				}
			}
			if( isset( $product->aioseop_menulabel ) && $product->aioseop_menulabel ) {
				if( $product_data->aioseop_menulabel <> $product->aioseop_menulabel ) {
					update_post_meta( $product->ID, '_aioseop_menulabel', $product->aioseop_menulabel );
					$product->updated = true;
				}
			}
		}
		return $product;

	}
	add_filter( 'wpsc_pd_merge_product_addons', 'wpsc_st_pd_merge_product_addons', null, 3 );

	/* End of: WordPress Administration */

}
?>